package com.freemyleft.left.zapp.selectimage.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.FileProvider;






import com.freemyleft.left.R;
import com.freemyleft.left.zapp.selectimage.FileUtils;
import com.freemyleft.left.zapp.selectimage.PhotoZoomUtils;
import com.freemyleft.left.zapp.selectimage.bean.Image;
import com.freemyleft.left.zapp.selectimage.listener.CameraListener;

import java.io.File;


public class  CameraActivity extends FragmentActivity {
    private static CameraListener mCameraListener;
    private String mImageName;
    private Uri mUri ;
    public static boolean mIsCameraFinish = false;
    private final int CAMERA = 0000;
    private final int ZOOM= 1000;
    private boolean mIsCut = false;
    private int mCutWidth = 300;
    private int mCutHight = 300;
    private File mPhotoFile;
    public static CameraActivity mActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        mIsCameraFinish = false;
        mActivity= CameraActivity.this;
        initView();
    }
    public static void setOnCameraListener(CameraListener cameraListener){
        mCameraListener = cameraListener;
    }
    private void initView() {
        mIsCut =getIntent().getBooleanExtra("isCut", false);
        mCutWidth = getIntent().getIntExtra("cut_width", 300);
        mCutHight = getIntent().getIntExtra("cut_hight", 300);
        mImageName = String.valueOf(System.currentTimeMillis()) + ".jpg";
        mPhotoFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), mImageName);
        takePhoto();
    }

    /**
     * 拍摄照片
     * */
    public void takePhoto(){
        Intent intent = null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0的方式
                mUri = FileProvider.getUriForFile(mActivity,  "com.freemyleft.left.left_app", mPhotoFile);
                intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
                mActivity.startActivityForResult(intent, CAMERA);
            }else{
                mUri = Uri.fromFile(mPhotoFile);
                intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is capture
                intent.putExtra("return-data", false);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra("noFaceDetection", true);
                mActivity.startActivityForResult(intent, CAMERA);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 拍照获取的
        if (resultCode == Activity.RESULT_OK && requestCode == CAMERA) {//拍照回调
            if (Uri.fromFile(mPhotoFile) != null) {
                Image image = FileUtils.getImage(CameraActivity.this, mPhotoFile.getPath(), mImageName);
                if (image.width == 0 && image.height == 0){
                    finish();
                    return;
                }
                if (mIsCut) {
                    PhotoZoomUtils.startPhotoZoom(mActivity,mUri, mPhotoFile, mCutWidth,mCutHight, ZOOM);
                } else {
                    if (mCameraListener != null) {
                        mCameraListener.cameraListener(image);
                    }
                    finish();
                }
            }
        }else if (resultCode == Activity.RESULT_OK && requestCode == ZOOM) {//裁剪
            Image image = FileUtils.getImage(CameraActivity.this, mPhotoFile.getPath(), mImageName);
            if (mCameraListener != null) {
                mCameraListener.cameraListener(image);
                finish();
            }
        }else {
            finish();
        }
    }


}
